<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    
    <title>登录示例</title>
    <style>
        .modal {
    display: none;
    position: fixed;
    z-index: 1;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    overflow: auto;
    background-color: rgba(0, 0, 0, 0.4);
}

.modal-content {
    background-color: #fefefe;
    margin: 15% auto;
    padding: 20px;
    border: 1px solid #888;
    width: 30%;
}

.close {
    color: #aaa;
    float: right;
    font-size: 28px;
    font-weight: bold;
}

.close:hover,
.close:focus {
    color: black;
    text-decoration: none;
    cursor: pointer;
}
    </style>
</head>

<body>
    <button id="loginBtn">登录</button>
    <div id="loginModal" class="modal">
        <div class="modal-content">
            <span class="close">&times;</span>
            <h2>登录</h2>
            <label for="username">账号：</label><input type="text" id="username" placeholder="请输入账号"><br>
            <label for="password">密码：</label><input type="password" id="password" placeholder="请输入密码"><br>
            <button id="submitBtn">提交</button>
        </div>
    </div>
    <script>
        // 获取登录按钮元素
const loginBtn = document.getElementById('loginBtn');
// 获取登录模态框元素
const loginModal = document.getElementById('loginModal');
// 获取关闭按钮元素
const closeBtn = document.querySelector('.close');

// 点击登录按钮时显示模态框
loginBtn.addEventListener('click', function () {
    loginModal.style.display = "block";
});

// 点击关闭按钮时隐藏模态框
closeBtn.addEventListener('click', function () {
    loginModal.style.display = "none";
});

// 点击模态框外部区域也可关闭模态框
window.addEventListener('click', function (event) {
    if (event.target === loginModal) {
        loginModal.style.display = "none";
    }
});
    </script>
</body>

</html>